package cn.hicard.risk.dao.bd.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import cn.hicard.core.Where;
import cn.hicard.core.Page;
import cn.hicard.core.PageResult;
import cn.hicard.risk.dao.BaseDao;
import cn.hicard.risk.dao.bd.CallorderDao;
import cn.hicard.risk.entity.BdCallorder;

@Repository
public class CallorderDaoImpl extends BaseDao<BdCallorder> implements CallorderDao {
	
	@Override
	public PageResult<BdCallorder> search(Where condition, Page page) throws Exception {
		String sql = "SELECT c.* FROM bd_callorder AS c LEFT JOIN bd_merch AS m ON c.merchno=m.id WHERE 1=1 ";
		String countSql = "SELECT COUNT(*) FROM bd_callorder AS c LEFT JOIN bd_merchinfo AS m ON c.merchno=m.hm_id WHERE 1=1 ";
		sql += condition.queryString();
		countSql += condition.queryString();
		
		PageResult<BdCallorder> result = new PageResult<BdCallorder>();
		result.setDataList(super.findBySqlPage(sql, page, condition.getParams()));
		result.setResultCount(super.findCountBySql(countSql, condition.getParams()));
		
		return result;
	}		

	@Override
	public void saveList(List<BdCallorder> list) throws Exception {
		super.save(list);
	}

	@Override
	public void updateList(List<BdCallorder> list) throws Exception {
		super.update(list);	
	}

	@Override
	public boolean existJzg(String orderno) throws Exception {
		String hql = "SELECT COUNT(*) FROM BdCallorder WHERE datatype='jzg' AND orderno=?";
		return super.findCountByHql(hql, orderno) > 0;
	}

	@Override
	public boolean existPos(int pkTradefileinfo) throws Exception {
		String hql = "SELECT COUNT(*) FROM BdCallorder WHERE datatype='pos' AND pkTradefileinfo=?";
		return super.findCountByHql(hql, pkTradefileinfo) > 0;
	}

	/**
	 * 根据条件查询交易数据集合
	 * @throws Exception 
	 */
	public List<BdCallorder> findList(BdCallorder start, BdCallorder end) throws Exception {
		
		String hql = "FROM BdCallorder WHERE 1=1";
		List<Object> params = new ArrayList<Object>();
		
		String merchno = start.getWpmerchno();
		String sysno = start.getSysno();
		String datatype = start.getDatatype();
		String checkstateStart = start.getCheckstate();
		String checkstateEnd = end.getCheckstate();
	
		
		String transdateStart = start.getTransdate();
		String transdateEnd = end.getTransdate();
		
		String procdateStart = start.getProcdate();
		String procdateEnd = end.getProcdate();
		
		if(merchno != null && merchno.length()>0){
			hql += " AND wpmerchno=?";
			params.add(merchno);
		}
		if(sysno != null && sysno.length()>0){
			hql += " AND sysno=?";
			params.add(sysno);
		}
		if( ! "all".equals(datatype)){
			hql += " AND datatype=?";
			params.add(datatype);
		}
		if(checkstateStart != null && checkstateStart.length()>0){
			hql += " AND checkstate in (?";
			params.add(checkstateStart);
			
		  if(checkstateEnd != null && checkstateEnd.length()>0){
			  hql += ",?";
			  params.add(checkstateEnd);
		  }			
			hql += ")";
		}
		if(transdateStart != null && transdateStart.length()>0){
			hql += " AND transdate>=?";
			params.add(transdateStart);
		}
		if(transdateEnd != null && transdateEnd.length()>0){
			hql += " AND transdate<=?";
			params.add(transdateEnd);
		}		
		if(procdateStart != null && procdateStart.length()>0){
			hql += " AND procdate>=?";
			params.add(procdateStart);
		}
		if(procdateEnd != null && procdateEnd.length()>0){
			hql += " AND procdate<=?";
			params.add(procdateEnd);
		}
				
		return super.findByHql(hql, params.toArray());
	}

	@Override
	public void update(BdCallorder callorder) throws Exception {
		super.update(callorder);		
	}
	
}
